/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package skv.db;

import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import skv.db.iface.DbHandler;

/**
 *Der MySQL Handler ist eine Implementation des DbHandler interface.
 * Es bindet die Vorgabe Methoden an eine im connectionstring spezifizierte
 * MySQL Datenbank.
 * @author Alex
 */
public class MysqlHandler implements DbHandler{

    Connection connection;
    private Statement stmt;
    String connectionstring;

    public MysqlHandler(String connectionstring) {
        this.connectionstring = connectionstring;
    }
    
    
    @Override
    public void connect() {
        try {
            connection = DriverManager.getConnection(connectionstring);
        } catch (SQLException ex) {
            Logger.getLogger(MysqlHandler.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public ResultSet doQuery(String sqlstring) {
        System.out.println(sqlstring);
        try {
            stmt = connection.createStatement();
            if(sqlstring.startsWith("INSERT") || sqlstring.startsWith("DELETE")){
                stmt.executeUpdate(sqlstring);
            }else{
                return stmt.executeQuery(sqlstring);
            }
            
        } catch (SQLException ex) {
            Logger.getLogger(MysqlHandler.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }

    @Override
    public void disconnect() {
        try {
            connection.close();
        } catch (SQLException ex) {
            Logger.getLogger(MysqlHandler.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
}
